package com.michaelbulava.TFS.vcs.internal;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.RepositoryLocation;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.history.VcsFileRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.michaelbulava.TFS.vcs.exceptions.TFSException;
import com.michaelbulava.TFS.vcs.revision.TFSContentRevision;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.Date;

/**
 * Created by MichaelA on 8/18/2015.
 */
public class TFSFileRevision implements VcsFileRevision{
    private final Project myProject;
    private final Date myDate;

    private byte[] myContent;
    private final String myCommitMessage;
    private final String myAuthor;
    private final int myItemId;
    private final int myChangeset;
    private final Workspace myWorkspace;

    public TFSFileRevision(Project project, Workspace workspace, int itemId, Date date, String commitMessage,
                           String author, int changeset){
        this.myProject = project;
        this.myWorkspace = workspace;
        this.myDate = date;
        this.myCommitMessage = commitMessage;
        this.myAuthor = author;
        this.myChangeset = changeset;
        this.myItemId = itemId;
    }

    @Nullable
    @Override
    public String getBranchName() {
        return null;
    }

    @Nullable
    @Override
    public RepositoryLocation getChangedRepositoryPath() {
        return null;
    }

    @Override
    public byte[] loadContent() throws IOException, VcsException {
        return this.myContent = createContentRevision().doGetContent();
    }

    @Nullable
    @Override
    public byte[] getContent() throws IOException, VcsException {
        return myContent;
    }

    @Override
    public VcsRevisionNumber getRevisionNumber() {
        return new VcsRevisionNumber.Int(myChangeset);
    }

    @Override
    public Date getRevisionDate() {
        return myDate;
    }

    @Nullable
    @Override
    public String getAuthor() {
        return myAuthor;
    }

    @Nullable
    @Override
    public String getCommitMessage() {
        return myCommitMessage;
    }



    public TFSContentRevision createContentRevision() throws VcsException{
        try{
            return TFSContentRevision.create(myProject, myWorkspace, myChangeset, myItemId);
        } catch (TFSException e) {
            throw new VcsException(e);
        }
    }
}
